package com.project;

import com.project.servlet.AddServlet;
import com.project.servlet.JsonServlet;
import com.project.servlet.TestServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

public class MainWeb1 {

    public MainWeb1(){
        Tomcat tomcat = new Tomcat();
        //设置tomcat启动端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //创建上下文
        Context context = tomcat.addContext("",null);

        try {
            //注册servlet
            Wrapper w = tomcat.addServlet(context,"testServlet",new TestServlet());

            //容器一启动就完成该Servlet初始化动作
            //该值为正数或是0时表示容器一启动就完成初始化
            //该值为负数或默认时，表示第一次访问该servlet完成初始化
            w.setLoadOnStartup(1);

            //设置客户端请求的url路径
            w.addMapping("/user/*"); //tt是url路径

            Wrapper wJson = Tomcat.addServlet(context,"json",new JsonServlet());
            wJson.addMapping("/json");

            Wrapper wadd = Tomcat.addServlet(context,"addProduct",new AddServlet());
            wadd.addMapping("/addProduct");

            //启动tomcat
            tomcat.start();

        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {

        new MainWeb1();
    }
}
